﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI;
using System.Web.UI.WebControls;
using UDS.Components;

namespace UDS.SubModule.CM.Stat
{
    /// <summary>
    /// Fee 的摘要说明。
    /// </summary>
    public partial class FeePage : Page
    {
        protected DateTime begintime;
        protected DateTime endtime;
        protected int totalfee;
        // private string type = "";

        #region Protected static methods

        protected static string GetLinkMan(DataRow[] row)
        {
            if (row.Length != 0)
            {
                return (row[0]["Name"].ToString());
            }
            return ("");
        }

        protected static string GetCurStatus(string curstatus)
        {
            switch (curstatus.Split(',')[0])
            {
                case "trace":
                    return ("跟踪");
                case "boot":
                    return ("启动");
                case "commend":
                    return ("产品推荐");
                case "requirement":
                    return ("需求定义");
                case "submit":
                    return ("方案提交");
                case "negotiate":
                    return ("商务谈判");
                case "actualize":
                    return ("项目实施");
                case "traceservice":
                    return ("跟踪服务");
                case "last":
                    return ("收尾款");
            }
            return ("");
        }

        protected static string GetAddMan(DataRow[] row)
        {
            if (row.Length != 0)
            {
                return (row[0]["realname"].ToString());
            }
            return ("");
        }

        protected int GetFee(DataRow[] row)
        {
            int fee = 0;
            if (row.Length != 0)
            {
                foreach (DataRow datarow in row)
                {
                    fee += Int32.Parse(datarow["fee"].ToString());
                }
                totalfee += fee;
                return (fee);
            }
            return (0);
        }

        protected static string GetTravel(int sellmanid, DateTime begintime, DateTime endtime)
        {
            var cm = new Components.CM();
            return (cm.GetTravelFeeBySellmanID(sellmanid, begintime, endtime).ToString());
        }

        protected static string GetFood(int sellmanid, DateTime begintime, DateTime endtime)
        {
            var cm = new Components.CM();
            return (cm.GetFoodFeeBySellmanID(sellmanid, begintime, endtime).ToString());
        }

        protected static string GetGift(int sellmanid, DateTime begintime, DateTime endtime)
        {
            var cm = new Components.CM();
            return (cm.GetGiftFeeBySellmanID(sellmanid, begintime, endtime).ToString());
        }

        protected static string GetOuter(int sellmanid, DateTime begintime, DateTime endtime)
        {
            var cm = new Components.CM();
            return (cm.GetOuterFeeBySellmanID(sellmanid, begintime, endtime).ToString());
        }

        protected static string GetContactTimes(int sellmanid, DateTime begintime, DateTime endtime)
        {
            var cm = new Components.CM();
            SqlDataReader dr = cm.GetContactBySellmanID(sellmanid, begintime, endtime);
            DataTable dt = Tools.ConvertDataReaderToDataTable(dr);
            return (dt.Rows.Count.ToString());
        }

        protected static string GetContactedClient(int sellmanid, DateTime begintime, DateTime endtime)
        {
            var cm = new Components.CM();
            SqlDataReader dr = cm.GetContactedClientBySellmanID(sellmanid, begintime, endtime);
            DataTable dt = Tools.ConvertDataReaderToDataTable(dr);
            return (dt.Rows.Count.ToString());
        }

        #endregion Protected static methods

        protected override void OnInit(EventArgs e)
        {
            ddl_order.SelectedIndexChanged += ddl_order_SelectedIndexChanged;
            btn_OK.Click += btn_OK_Click;
            base.OnInit(e);
        }

        protected override void OnLoad(EventArgs e)
        {
            // 在此处放置用户代码以初始化页面
            string type;
            if (!Page.IsPostBack)
            {
                begintime = (Request.QueryString["begintime"] == null)
                                ? DateTime.Now
                                : DateTime.Parse(Request.QueryString["begintime"]);
                endtime = (Request.QueryString["endtime"] == null)
                              ? DateTime.Now
                              : DateTime.Parse(Request.QueryString["endtime"]);
                type = (Request.QueryString["type"] == null) ? "" : Request.QueryString["type"];
                ViewState["begintime"] = begintime;
                ViewState["endtime"] = endtime;
                ViewState["type"] = type;

                tbx_begintime.Text = begintime.ToShortDateString();
                tbx_endtime.Text = endtime.ToShortDateString();

                foreach (ListItem li in ddl_order.Items)
                {
                    if (li.Value == type)
                    {
                        li.Selected = true;
                    }
                    else
                    {
                        li.Selected = false;
                    }
                }
                if (type != "") BindData(type);
            }
            else
            {
                begintime = DateTime.Parse(ViewState["begintime"].ToString());
                endtime = DateTime.Parse(ViewState["endtime"].ToString());
                // type = ViewState["type"].ToString();
            }

            base.OnLoad(e);
        }

        private void BindData(string type)
        {
            begintime = DateTime.Parse(tbx_begintime.Text);
            endtime = DateTime.Parse(tbx_endtime.Text);

            var cm = new Components.CM();
            var ds = new DataSet();
            var staff = new Components.Staff();

            SqlDataReader dr_staff = staff.GetAllStaffs();
            DataTable dt_staff = Tools.ConvertDataReaderToDataTable(dr_staff);
            dt_staff.TableName = "Staff";
            ds.Tables.Add(dt_staff);

            SqlDataReader dr_client = cm.GetContactedClientBySellmanID(0, begintime, endtime);
            DataTable dt_client = Tools.ConvertDataReaderToDataTable(dr_client);
            dt_client.TableName = "Client";
            ds.Tables.Add(dt_client);

            ltl_Client.Text = dt_client.Rows.Count.ToString();

            if (type == "client") //按客户排列
            {
                dgrd_clientfee.Visible = true;
                dgrd_sellmanfee.Visible = false;

                SqlDataReader dr_contact = cm.GetContactInfo(begintime, endtime);
                DataTable dt_contact = Tools.ConvertDataReaderToDataTable(dr_contact);
                dt_contact.TableName = "Contact";
                ds.Tables.Add(dt_contact);

                SqlDataReader dr_linkman = cm.GetAllLinkman();
                DataTable dt_linkman = Tools.ConvertDataReaderToDataTable(dr_linkman);
                dt_linkman.TableName = "Linkman";
                ds.Tables.Add(dt_linkman);

                ds.Relations.Add("ClientAddMan_Staff", ds.Tables["Client"].Columns["AddManID"],
                                 ds.Tables["Staff"].Columns["Staff_ID"], false);
                ds.Relations.Add("ClientLinkman_Staff", ds.Tables["Client"].Columns["ChiefLinkmanID"],
                                 ds.Tables["Linkman"].Columns["ID"], false);
                ds.Relations.Add("Client_Contact", ds.Tables["Client"].Columns["ID"],
                                 ds.Tables["Contact"].Columns["ClientID"], false);

                dgrd_clientfee.DataSource = dt_client.DefaultView;
                dgrd_clientfee.DataBind();

                ltl_Fee.Text = totalfee.ToString();
            }
            else if (type == "sellman")
            {
                dgrd_clientfee.Visible = false;
                dgrd_sellmanfee.Visible = true;

                SqlDataReader dr_sellman = cm.GetSellman(begintime, endtime);
                DataTable dt_sellman = Tools.ConvertDataReaderToDataTable(dr_sellman);
                dt_sellman.TableName = "Sellman";
                ds.Tables.Add(dt_sellman);

                foreach (DataRow row in dt_sellman.Rows)
                {
                    totalfee += Int32.Parse(row["fee"].ToString());
                }

                ds.Relations.Add("StaffID_RealName", ds.Tables["Sellman"].Columns["Staff_Name"],
                                 ds.Tables["Staff"].Columns["Staff_Name"], false);

                dgrd_sellmanfee.DataSource = dt_sellman.DefaultView;
                dgrd_sellmanfee.DataBind();

                ltl_Fee.Text = totalfee.ToString();
            }
        }

        private void btn_OK_Click(object sender, EventArgs e)
        {
            BindData(ddl_order.SelectedItem.Value);
        }

        private void ddl_order_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (((DropDownList)sender).SelectedItem.Value == "client")
            {
                dgrd_clientfee.Visible = true;
                dgrd_sellmanfee.Visible = false;
            }
            else if (((DropDownList)sender).SelectedItem.Value == "sellman")
            {
                dgrd_clientfee.Visible = false;
                dgrd_sellmanfee.Visible = true;
            }
            BindData(((DropDownList)sender).SelectedItem.Value);
        }
    }
}